package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.dao.StarPhotoDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.pojo.StarPhotoInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.StarPhotoService;

@Service("starPhotoService")
public class StarPhotoServiceImpl implements StarPhotoService {

	@Resource(name = "starPhotoDao")
	private StarPhotoDao starPhotoDao;

	@Override
	public StarPhotoInfo queryByPrimartKey(String cid) {
		return starPhotoDao.getByKey(cid);
	}

	@Override
	public Pagination<StarPhotoInfo> queryPage(StarPhotoInfo photoInfo) {
		if (StringUtils.isNotEmpty(photoInfo.getOrderBy())
				&& photoInfo.getOrderType() != null) {
			if (photoInfo.getOrderType() == OrderType.asc) {
				photoInfo.setPage_orderByAsc(photoInfo.getOrderBy());

			} else {
				photoInfo.setPage_orderByDesc(photoInfo.getOrderBy());
			}
		}
		Integer count = starPhotoDao.totalCount(photoInfo);
		photoInfo.calculate();
		List<StarPhotoInfo> list = starPhotoDao.queryList(photoInfo);
		Pagination<StarPhotoInfo> page = starPhotoDao.queryPagination(count,
				list, photoInfo);
		return page;
	}

	@Override
	public int save(StarPhotoInfo photoInfo, UserInfoVO currentUser) {
		photoInfo.setPublishTime(new Date());
		photoInfo.setUserId(currentUser.getUserId());
		return starPhotoDao.insert(photoInfo);
	}

	@Override
	public int update(StarPhotoInfo photoInfo, UserInfoVO currentUser) {
		photoInfo.setUpdateTime(new Date());
		photoInfo.setUserId(currentUser.getUserId());
		return starPhotoDao.update(photoInfo);
	}

	@Override
	public void savePhotos(List<StarPhotoInfo> photoInfos) {
		starPhotoDao.batchInsert(photoInfos);
	}

}
